package com.xianbeiying.app.ckgl.model;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.bean.JsonBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpPostManger;
import com.xianbeiying.app.ckgl.util.DialogUtile;
import com.xianbeiying.app.ckgl.util.LogUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;

import cz.msebera.android.httpclient.Header;

/**
 * 删除网络访问
 */
public class DeleteModel extends BaseModel{

    private AsyncHttpClient client;
    private RequestParams params;
    private Context context;
    public DeleteModel(Context context){
        this.context=context;
        client = new AsyncHttpClient();
        params = new RequestParams();
    }

    /**
     *删除生长周期
     * CROPID：作物id，PLANTTYPE：育苗或种植
     */
    public  void getDELETE_CROPSTEP(String k, String CROPID,String CROPTYPE,String PLANTTYPE,boolean isDiaylog){
        if(isDiaylog){
            DialogUtile.showLoading(context);
        }
        String url=JsonBean.deleteGrowZq(k,CROPID,CROPTYPE,PLANTTYPE);
        com.lidroid.xutils.util.LogUtils.e("http://"+MyApplication.IP+UrlBean.GET_CROP_STEP_ALL_INFO+url);
       HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.GET_CROP_STEP_ALL_INFO,url , new AsyncHttpResponseHandler(){
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            //关闭加载
            DialogUtile.closeDialog();
//            LogUtils.e("url",UrlBean.LOGIN+JsonBean.loginjsonString(username,password)+"======"+headers);
//            LogUtils.e("返回值", new String(responseBody) + "----------------"+statusCode);
             Log.e("返回值",new String(responseBody));
            //函数返回值
            DeleteModel.this.onCallBackData("删除生长周期",responseBody);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            //关闭加载
            DialogUtile.closeDialog();
            LogUtils.e("返回值", "----------------"+statusCode);
            if(statusCode==0){
                ToastUtil.showInfo(context,"无法连接平台");
            }else if(statusCode==302){
                MyApplication.isLogin=false;
//                ToastUtil.showInfo(context,"请先登录");
            }else{
                ToastUtil.showInfo(context,"无法连接平台");
            }
        }
    });

}
    //
    /**
     * 删除周期性计划
     * SYSTEMID:系统编号
     * CODE:计划类型编号，值为0
     */
    public void DeleteQUERY_CYCLEPLANS(String k,long SYSTEMID, int CODE, boolean isDiaylog) {
        if (isDiaylog) {
            DialogUtile.showLoading(context);
        }
        String url=JsonBean.zqPlantString(k,SYSTEMID,CODE);
        LogUtils.e("删除周期",UrlBean.QUWEYDEVSDEFAULT+url+"======");
        HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.QUWEYDEVSDEFAULT,url, new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //关闭加载
//                DialogUtile.closeDialog();
                Log.e("删除周期",new String(responseBody));
                //函数返回值
                DeleteModel.this.onCallBackData("删除周期",responseBody);
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                LogUtils.e("返回值", "----------------"+statusCode);
                if(statusCode==0){
                    ToastUtil.showInfo(context,"无法连接平台");
                }else if(statusCode==302){
                    MyApplication.isLogin=false;
//                    ToastUtil.showInfo(context,"请先登录");
                }else{
                    ToastUtil.showInfo(context,"无法连接平台");
                }
            }
        });
    }

    //删除自动性计划
    public void DeleteQUERY_TRIGGERPLANS(String k,long SYSTEMID, int CODE, boolean isDiaylog) {
        if (isDiaylog){
            DialogUtile.showLoading(context);
        }
        String url=JsonBean.zqPlantString(k,SYSTEMID,CODE);
        LogUtils.e("删除自动",UrlBean.QUWEYDEVSDEFAULT+url+"======");
        HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.QUWEYDEVSDEFAULT,url, new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //关闭加载
//                DialogUtile.closeDialog();
                Log.e("删除自动",new String(responseBody));
                //函数返回值
                DeleteModel.this.onCallBackData("删除自动",responseBody);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                LogUtils.e("返回值", "----------------"+statusCode);
                if(statusCode==0){
                    ToastUtil.showInfo(context,"无法连接平台");
                }else if(statusCode==302){
                    MyApplication.isLogin=false;
//                    ToastUtil.showInfo(context,"请先登录");
                }else{
                    ToastUtil.showInfo(context,"无法连接平台");
                }
            }
        });
    }
    //删除我的收藏
    public void DeleteCollect(String k,int id,boolean isDiaylog) {
        if (isDiaylog){
            DialogUtile.showLoading(context);
        }
        String url=JsonBean.deleteCollect(k,id);
        LogUtils.e("删除收藏","http://"+MyApplication.IP+UrlBean.DELETE_COLLECTION+url+"======");
        HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.DELETE_COLLECTION,url, new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //关闭加载
                DialogUtile.closeDialog();
                Log.e("删除收藏",new String(responseBody));
                //函数返回值
                DeleteModel.this.onCallBackData("删除收藏",responseBody);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                LogUtils.e("返回值", "----------------"+statusCode);
                if(statusCode==0){
                    ToastUtil.showInfo(context,"无法连接平台");
                }else if(statusCode==302){
                    MyApplication.isLogin=false;
//                    ToastUtil.showInfo(context,"请先登录");
                }else{
                    ToastUtil.showInfo(context,"无法连接平台");
                }
            }
        });
    }
    //删除我的消息
    public void DeleteMessage(String k,int serial,boolean isDiaylog) {
        if (isDiaylog){
            DialogUtile.showLoading(context);
        }
        String url=JsonBean.deletemMessage(k,serial);
        LogUtils.e("删除消息","http://"+MyApplication.IP+UrlBean.DELETE_MESSAGE+url+"======");
        HttpPostManger.post(context,"http://"+MyApplication.IP+UrlBean.DELETE_MESSAGE,url, new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //关闭加载
                DialogUtile.closeDialog();
                Log.e("删除消息",new String(responseBody));
                //函数返回值
                DeleteModel.this.onCallBackData("删除消息",responseBody);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                LogUtils.e("返回值", "----------------"+statusCode);
                if(statusCode==0){
                    ToastUtil.showInfo(context,"无法连接平台");
                }else if(statusCode==302){
                    MyApplication.isLogin=false;
//                      ToastUtil.showInfo(context,"请先登录");
                }else{
                    ToastUtil.showInfo(context,"无法连接平台");
                }
            }
        });
    }
}
